﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class PersonInClass
    {
        private List<Person> lstPerson = new List<Person>();
        public List<Person> LstPerson { get { return lstPerson; } set { lstPerson = value; } }

        private string className = string.Empty;
        public string ClassName { get { return className; } set { className = value; } }
    }

    class Person
    {
        private int id = 0;
        private string name = string.Empty;

        public int ID { get { return id; } set { id = value; } }
        public string Name { get { return name; } set { name = value; } }

        public override string ToString()
        {
            return id.ToString();
        }
    }

    public partial class Form1 : Form
    {
        private List<PersonInClass> lstClass = new List<PersonInClass>();

        public Form1()
        {
            InitializeComponent();
            PersonInClass p1 = new PersonInClass();
            p1.LstPerson.Add(new Person() { ID = 1, Name = "test1" });
            p1.LstPerson.Add(new Person() { ID = 2, Name = "test2" });
            p1.ClassName = "class1";
            lstClass.Add(p1);

            PersonInClass p2 = new PersonInClass();
            p2.LstPerson.Add(new Person() { ID = 10, Name = "test10" });
            p2.LstPerson.Add(new Person() { ID = 20, Name = "test20" });
            p2.ClassName = "class2";
            lstClass.Add(p2);


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = lstClass;
            
            //this.dataGridView1.AutoGenerateColumns = false;
            //this.dataGridView1.Columns.Clear();
            //dataGridView1.AllowUserToAddRows = false;

            //DisplayCol(dataGridView1, "Person.Name", "Person.Name", false); 
        }

        void DisplayCol(DataGridView dgv, String dataPropertyName, String headerText, bool readOnly)
        {
            dgv.AutoGenerateColumns = false;
            DataGridViewTextBoxColumn obj = new DataGridViewTextBoxColumn();
            obj.ReadOnly = readOnly;
            obj.DataPropertyName = dataPropertyName;
            obj.HeaderText = headerText;
            obj.Name = dataPropertyName;
            obj.Resizable = DataGridViewTriState.True;
            dgv.Columns.AddRange(new DataGridViewColumn[] { obj });
        }
    }


}
